import _ from '../../../utils/under-dash';
import BaseXform from '../base-xform';

function booleanToXml(model) {
    return model ? '1' : undefined;
}

class PrintOptionsXform extends BaseXform {
    get tag() {
        return 'printOptions';
    }

    render(xmlStream, model) {
        if (model) {
            const attributes = {
                headings: booleanToXml(model.showRowColHeaders),
                gridLines: booleanToXml(model.showGridLines),
                horizontalCentered: booleanToXml(model.horizontalCentered),
                verticalCentered: booleanToXml(model.verticalCentered),
            };
            if (_.some(attributes, value => value !== undefined)) {
                xmlStream.leafNode(this.tag, attributes);
            }
        }
    }

    parseXmlObject(node) {
        switch (node._name) {
            case this.tag:
                this.model = {
                    showRowColHeaders: node._attributes.headings === '1',
                    showGridLines: node._attributes.gridLines === '1',
                    horizontalCentered: node._attributes.horizontalCentered === '1',
                    verticalCentered: node._attributes.verticalCentered === '1',
                };
                return true;
            default:
                return false;
        }
    }

    parseOpen(node) {
        switch (node.name) {
            case this.tag:
                this.model = {
                    showRowColHeaders: node.attributes.headings === '1',
                    showGridLines: node.attributes.gridLines === '1',
                    horizontalCentered: node.attributes.horizontalCentered === '1',
                    verticalCentered: node.attributes.verticalCentered === '1',
                };
                return true;
            default:
                return false;
        }
    }

    parseText() {
    }

    parseClose() {
        return false;
    }
}

export default PrintOptionsXform;
